﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace DrawingTest
{
    class Drawtest1
    {

        private Pen m_bluePen = new Pen(Color.Blue,1);
        private Graphics m_dc;
        PointF[] m_inputxyList;
        SizeF m_windowssize;

        public Drawtest1(Graphics dc, PointF[] inputxylist, SizeF windowsSize)
        {
            m_dc = dc;
            m_inputxyList = inputxylist;
            m_windowssize = windowsSize;
            //dc.
        }

        public void draw()
        {

            m_dc.DrawLines(m_bluePen, transformation(m_inputxyList));
        }

        RectangleTX getBoundingBox(PointF[] inputxylist)
        {
            RectangleTX boundingbox = new RectangleTX();
            if (inputxylist==null)
            {
                throw new Exception("at least on point put into the getboundingbox");
            }
            float minx=inputxylist[0].X;
            float miny = inputxylist[0].Y;
            float maxx = minx;
            float maxy = miny;

            foreach (PointF point in inputxylist)
            {
                minx = minx < point.X ? minx : point.X;
                maxx = maxx > point.X ? maxx : point.X;
                miny = miny < point.Y ? miny : point.Y;
                maxy = maxy > point.Y ? maxy : point.Y;

            }

            boundingbox.leftbottom=new PointF(minx,miny);
            boundingbox.righttop = new PointF(maxx,maxy);

            return boundingbox;
        }

        PointF[] transformation(PointF[] inputxylist)
        {
            List<PointF> pointlist = new List<PointF>();
            // get the bounding box
            // TODO should be extracted to make this transformation more 
            // sharable
            RectangleTX rectangleSize = getBoundingBox(inputxylist);

            PointF lefttop = new PointF(rectangleSize.leftbottom.X,rectangleSize.righttop.Y);
            PointF leftdown = new PointF(rectangleSize.leftbottom.X,rectangleSize.leftbottom.Y);
            float boundingboxWidth = rectangleSize.righttop.X-rectangleSize.leftbottom.X;
            float boundingboxHeight = rectangleSize.righttop.Y-rectangleSize.leftbottom.Y;


            foreach (PointF point in inputxylist)
            {
                //point = point - lefttop;
                float currentX = point.X - leftdown.X;
                float currentY = point.Y - leftdown.Y;
                pointlist.Add(new PointF(currentX, currentY));
            }

            float xscale = m_windowssize.Width/boundingboxWidth;
            float yscale = m_windowssize.Height/boundingboxHeight;

            List<PointF> postPointlist = new List<PointF>();
            // resize to the windows W H
            foreach (PointF point in pointlist)
            {
                PointF currentPoint = new PointF(
                    point.X * (xscale),
                    m_windowssize.Height - point.Y * (yscale));

                postPointlist.Add(currentPoint);
            }

            return postPointlist.ToArray();
        }
    }

    class RectangleTX
    {
        public PointF leftbottom;
        public PointF righttop;
    }
}
